package cc.rengu.redp.flowable.service.impl;

import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpConstant;
import cc.rengu.redp.common.utils.RedpUtil;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.flowable.dao.FormDesignMapper;
import cc.rengu.redp.flowable.domain.FormDesign;
import cc.rengu.redp.flowable.service.FormDesignService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("formDesignService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FormDesignServiceImpl extends ServiceImpl<FormDesignMapper, FormDesign> implements FormDesignService {

    /**
     * 无参数：查询全部   formId：单一查询   name：模糊查询
     * @param formDesign formDesign
     * @param request  request
     * @return  List<FormDesign>
     */
    @Override
    public IPage<FormDesign> formQuery(FormDesign formDesign, QueryRequest request) {
        try {
            Page<FormDesign> page = new Page<>();
            SortUtil.handlePageSort(request, page, null, RedpConstant.ORDER_DESC, false);
            LambdaQueryWrapper<FormDesign> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(formDesign.getFormId())) {
                lambdaQueryWrapper.eq(FormDesign::getFormId, formDesign.getFormId());
            }
            if (StringUtils.isNotBlank(formDesign.getFormName())) {
                lambdaQueryWrapper.like(FormDesign::getFormName, formDesign.getFormName());
            }
            lambdaQueryWrapper.orderByDesc(FormDesign::getCreatedDate);
            return this.page(page, lambdaQueryWrapper);
        } catch (Exception e) {
            log.error("查询表单异常", e);
            return null;
        }
    }

    @Override
    public List<FormDesign> formQueryAll() {
        return this.baseMapper.selectByMap(null);
    }

    /**
     * 表单设计新增
     *
     * @param formDesign 实体类
     */
    @Override
    public void addForm(FormDesign formDesign) {
        // 设置创建时间
        formDesign.setCreatedDate(new Date());
        formDesign.setCreatedBy(RedpUtil.getCurrentUser().getUsername());
        save(formDesign);
    }

    /**
     * 表单设计修改
     * @param formDesign 需要修改的表单数据
     */
    @Override
    public void modifiedForm(FormDesign formDesign) {
        updateById(formDesign);
    }

    /**
     * 表单删除
     * @param formIds 表单Id
     */
    @Override
    public void deleteForm(String[] formIds) {
        List<String> list = Arrays.asList(formIds);
        removeByIds(list);
    }

    /**
     * 预览表单
     * @return String
     */
    @Override
    public FormDesign getForm(String formId) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<FormDesign>().eq(FormDesign::getFormId, formId));
    }
}
